package sd.czw.com.mmgapp.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.zhy.http.okhttp.OkHttpUtils;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.model.MyLoginUserInfoCallback;
import sd.czw.com.mmgapp.model.UserInfo;
import sd.czw.com.mmgapp.utils.SpUtils;

;

/**
 * Created by czw
 * desc 描述：登录界面
 * on 2017/7/13.
 */
public class LoginActivity extends AppCompatActivity {

    //====
    @Bind(R.id.tv_qx)
    TextView tvQx;
    @Bind(R.id.edt_username)
    EditText edtUsername;
    @Bind(R.id.edt_password)
    EditText edtPassword;
    @Bind(R.id.btn_login)
    Button btnLogin;
    @Bind(R.id.tv_forgetpw)
    TextView tvForgetpw;
    @Bind(R.id.tv_sign)
    TextView tvSign;
    private String username,password;
    private String flag_activity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        edtUsername.addTextChangedListener(watcher);
        edtPassword.addTextChangedListener(watcher);
        flag_activity = getIntent().getStringExtra("flag_activity");
    }



    @OnClick({R.id.tv_forgetpw,R.id.btn_login,R.id.tv_sign,R.id.tv_qx})
    public void onclcik(View view){
        switch (view.getId()){
            case R.id.tv_qx:
                finish();
                overridePendingTransition(R.anim.push_bottom_in1,R.anim.push_bottom_out1);
                break;
            case R.id.tv_forgetpw:
                startActivity(new Intent(this,GetXGCodeActivity.class));
                break;
            case R.id.tv_sign:
                startActivity(new Intent(this,GetCodeActivity.class));
                break;
            case R.id.btn_login:
                String pw = edtPassword.getText().toString().trim();
                String un = edtUsername.getText().toString().trim();
                if (pw != null && un != null && !("".equals(pw)) && !("".equals(un))) {
                    OkHttpUtils.post().url(Contants.NMLOGIN).addParams("username", un).addParams("password", pw).addParams("act", "login")
                            .addParams("time", Contants.Time).addParams("sign", Contants.Sign)
                            .build().execute(new MyLoginUserInfoCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                        }

                        @Override
                        public void onResponse(UserInfo response, int id) {
                            System.out.println("login_code:"+response.getErrcode());
                            if (response.getErrcode() != 1){
                                Toast.makeText(LoginActivity.this, ""+response.getErrmsg()+"====", Toast.LENGTH_SHORT).show();
                            }else if (response.getErrcode() == 1){
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                SpUtils.setToken(LoginActivity.this).edit().putString("token",response.getErrmsg().getToken()).commit();
                                SpUtils.setUsername(LoginActivity.this).edit().putString("username",response.getErrmsg().getUsername().toString()).commit();
                                System.out.println("username:"+response.getErrmsg().getUsername());
                                System.out.println("token="+response.getErrmsg().getToken());
                                finish();
                                //overridePendingTransition(R.anim.push_bottom_in1,R.anim.push_bottom_out1);
                            }else {
                                Toast.makeText(LoginActivity.this, ""+response.getErrmsg()+"====", Toast.LENGTH_SHORT).show();
                            }
//                            if (response.getErrcode() == 1) {
//                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//                                SpUtils.setToken(LoginActivity.this).edit().putString("token",response.getErrmsg().getToken()).commit();
//                                SpUtils.setUsername(LoginActivity.this).edit().putString("username",response.getErrmsg().getUsername().toString()).commit();
//                                System.out.println("username:"+response.getErrmsg().getUsername());
//                                System.out.println("token="+response.getErrmsg().getToken());
//                                finish();
//                                overridePendingTransition(R.anim.push_bottom_in1,R.anim.push_bottom_out1);
//                            } else {
//                                Toast.makeText(LoginActivity.this, response.getErrmsg() + ",请确认账号密码无误", Toast.LENGTH_SHORT).show();
//                            }
                        }
                    });
                }else {
                    Toast.makeText(LoginActivity.this, "密码和用户名不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            finish();
            overridePendingTransition(R.anim.push_bottom_in1,R.anim.push_bottom_out1);
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);//解除绑定，官方文档只对fragment做了解绑
    }

    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            username = edtUsername.getText().toString();
            password = edtPassword.getText().toString();
            if (username != null && password != null && !("".equals(username)) && !("".equals(password))){
                btnLogin.setBackground(ContextCompat.getDrawable(LoginActivity.this,R.drawable.login_checkbtn));
            }else {
                btnLogin.setBackground(ContextCompat.getDrawable(LoginActivity.this,R.drawable.login_btn));
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
}
